'''
Created on Nov 3, 2011

@author: alebalbin
'''

def read_net(file,netname):
    ifile=open(file)
    of=open(file+'.NNF','w')
    interaction='pp'
    netnode='%s\t%s\tpp\t%s'
    
    for l in ifile:
        f=l.strip('\n').split('\t')
        s,t=f[0],f[1]
        of.write( netname+'\t'+s+'\n')
        of.write( netname+'\t'+t+'\n')
        of.write( netnode%(s,s+'_t',s+'_p') +'\n')
        of.write( netnode%(s,s+'_p',s+'_pp') + '\n')
        of.write( netnode%(t,t+'_t',t+'_p') +'\n')
        of.write( netnode%(t,t+'_p',t+'_pp') + '\n')
        of.write( netname+'\t'+s+'\t'+'im'+'\t'+t+'\n')

    of.close()
    
def matrix_nnf(mat):
    '''
    Assume transcript=0,protein=0,phospho=1
    '''
    ifile =open(mat)
    of =open(mat+'_nnf.tsv','w')
    tp=["_t","_p","_pp"]
    ifile.next()
    of.write("nodeFoldChange\t(class=Double)\n")
    for l in ifile:
        f=l.strip('\r\n').split('\t')
        g=f[0]
        for i,j in zip(f[1:],tp):
            v= i if i != 'NAN' else '0'
            of.write(g+j+'='+v+'\n')
    of.close()

file='/Users/alebalbin/Desktop/nsclc_kras_dep/Data/scripts/hsa04010_edges.tab'
matfile='/Users/alebalbin/Documents/projects/nsclc_kras_dep/Data/phosphoproteomics/phosphoproteomicsdatasets/H2122_dep/phospho_flow_trans_FoldChange_merged_mat.tsv'
netname='hsa04010'
read_net(file,netname) 
matrix_nnf(matfile)


